package com.cyam.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cyam.domain.Comments;
import com.cyam.domain.Message;
import com.cyam.service.CommentsService;
import com.cyam.service.MessageService;
import com.cyam.util.Pager;

/**
 * 留言 controller
 * @author CourageAQ
 *
 */
@Controller
@RequestMapping("/message")
public class MessageController {
	
	@Resource
	private MessageService messageService;
	
	@Resource
	private CommentsService commentsService;
	
	/**
	 * 用于查询留言数，并分页
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String view(Model model,@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception {
		
		Pager pages = messageService.countPager(null, page, size);
		/*Map<String, Object> map = new HashMap<String,Object>();	
		Pager result = messageService.findPager(map, page, size);
		@SuppressWarnings("unchecked")
		List<Message> messages = result.getItem();
		for (Message message : messages) {
			map.put("m_id",message.getId());
			message.setComs(commentsService.findAll(map));
		}
		model.addAttribute("list", messages);*/
		model.addAttribute("pages", pages);
		return "message";
	}
	
	
	@RequestMapping("/listpage")
	@ResponseBody
	public JSONObject viewContent(@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Pager pages = messageService.findPager(map, page, size);
		@SuppressWarnings("unchecked")
		List<Message> messages = pages.getItem();
		for (Message message : messages) {
			map.put("m_id",message.getId());
			message.setComs(commentsService.findAll(map));
		}
		result.put("result", pages);
		return result;
	}
	
	@RequestMapping("/liuyan")
	@ResponseBody
	public String liuyan(Message message) {
		try {
			message.setClick_num(0);
			message.setMess_time(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
			messageService.save(message);	
			return "success";
		} catch (Exception e) {
			return "fail";
		}
	}
	
	
	/**
	 * 留言赞
	 * @throws Exception 
	 */
	@RequestMapping("/praise")
	@ResponseBody
	public String updateClick(Message message) {
		try {
			messageService.update(message);	
			return "success";
		} catch (Exception e) {
			return "fail";
		}
	}
	
	/**
	 * 用户评论
	 * @param message
	 * @return
	 */
	@RequestMapping("/addcoms")
	@ResponseBody
	public String comment(Comments comments) {
		try {
			commentsService.save(comments);	
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}
	
	
	
}
